#include<vector>
#include<unordered_map>
#include<iostream>
using namespace std;

int reduce(vector<int>& nums, int last, unordered_map<int, int>map) {
	if (last < 2) return 0;
	int cnt = 0;
	int idx = map[last];
	if (idx+1 != last) {
		++cnt;
		swap(nums[idx], nums[last-1]);
	}
	cnt += reduce(nums, last - 1, map);
	return cnt;
}

int getMin(vector<int>& nums) {
	unordered_map<int, int>map;
	for (int i = 0; i < nums.size(); ++i)
		map[nums[i]] = i;
	return reduce(nums, nums.size(), map);
}

int main() {
	vector<int>nums = { 4,3,2,1 };
	cout << getMin(nums) << endl;
}